package com.dk.controller;

import com.dk.pojo.TabCategory;
import com.dk.pojo.TabRoute;
import com.dk.pojo.TabRouteVO;
import com.dk.pojo.TabSeller;
import com.dk.service.CategoryService;
import com.dk.service.SellerService;
import com.dk.service.TravelRouteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("travelRoute")
public class TravelRouteController {
    @Autowired
    TravelRouteService travelRouteService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SellerService sellerService;

    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String, Object> getInfo(TabRouteVO tabRoute,
                                       @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<TabRouteVO> pageInfo = travelRouteService.getInfo(tabRoute,pageNumber,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());//总条数
        map.put("rows", pageInfo.getList());//数据
        return map;
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TabSeller> sellers = sellerService.getInfo();
        List<TabCategory> categories = categoryService.getInfo();
        model.addAttribute("categories",categories);
        model.addAttribute("sellers",sellers);
        return "travelRoute/add";
    }
    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable(value = "id")Integer id, Model model){
        TabRoute tabRoute = travelRouteService.getInfoById(id);
        List<TabSeller> sellers = sellerService.getInfo();
        List<TabCategory> categories = categoryService.getInfo();
        model.addAttribute("categories",categories);
        model.addAttribute("sellers",sellers);
        model.addAttribute("tabRoute",tabRoute);
        return "travelRoute/update";
    }
    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(HttpServletRequest request,String[] bigPic, TabRoute tabRoute){
        String newMessage = (String) request.getAttribute("newMessage");
        tabRoute.setRouteintroduce(newMessage);
        int i = travelRouteService.insertInfo(bigPic,tabRoute);

        System.out.println(newMessage);
        return i>0;
    }

    @RequestMapping("updateRoute")
    @ResponseBody
    public boolean updateRoute(HttpServletRequest request,String[] bigPic,TabRoute tabRoute){
        String newMessage = (String) request.getAttribute("newMessage");
        tabRoute.setRouteintroduce(newMessage);
        int i = travelRouteService.updateRoute(bigPic,tabRoute);
        return i>0;
    }
    @RequestMapping("deleteRouteById/{id}")
    @ResponseBody
    public boolean deleteRouteById(@PathVariable("id")Integer id){
        int i = travelRouteService.deleteRouteById(id);
        return i>0;
    }

}
